<?php session_start(); ?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 2</title>
</head>

<body>
<?php
$db="m1_f4502769";

include "db_connect.php";

/*
$con = mysql_connect("localhost","root","root") or die(mysql_error());
		mysql_select_db($db);
*/	
	
	
//echo $_SESSION['table'];

/**
hier heb ik eentje van tevoren gedaan omdat de fetch_row statment voor een of ander reden 
de eerst overslaat;
*/
$tablequery = mysql_query("SHOW TABLES FROM ".$db) or die("can tabellen niet ophalen");
$tableresult = mysql_fetch_array($tablequery ) or die("can tabellen niet creeren");

echo "<table border='2'><form method='get' action=''>";
echo "<tr><td><input name='Radio1' type='radio' value=".$tableresult[0]." ></td><td> ".$tableresult[0]."</td></tr>";
	while ($row = mysql_fetch_row($tablequery)) {
		$i=0;
	    echo "<tr><td><input name='Radio1' type='radio' value=".$row[$i]."></td><td>".$row[$i]."</td></tr>";
	    $i++;
	}
echo "<tr><td><input name='Submit1' type='submit' value='submit' /></form><td></tr></table></br>";


$table;
/*
if(!isset($_POST['Submit1'])||!isset($_GET['Radio1']) ){
$_SESSION['table'] = $tableresult[0];
}
*/
	
if(isset($_GET['Radio1'])){
//	echo"get ja </br>"; 
	$_SESSION['table'] = $_GET['Radio1'];
}	

if(isset($_SESSION['table'])){
$table = $_SESSION['table'];
}
//echo $_SESSION['table']."</br>";

$offset = 0;
$rowcount= 5;

//echo $table;

/**
dit vooert de query uit zoals delete en *update* met de post variables 
verkregen door de onderstande gerenerende formulier
*/
if (isset($_POST['operation'])){
//		echo"126</br>";
		$P_key = array_keys($_POST);
		//echo count($P_key);
		
		$delquery =" DELETE FROM  ".$table." WHERE ";
		$updquery ="UPDATE ".$table." SET ";
		$insquery = "INSERT INTO ".$table."	 VALUES";
		
			if(isset($_POST['operation'])){
			
//			echo"if ja</br>";
			
				if($_POST['operation']=='del'){
				$delquery = $delquery ." ". $P_key[0]." = '".$_POST[$P_key[0]]."'";

					for($i = 1;$i<(count($P_key)-2); $i++ ){
					$delquery = $delquery ." AND ". $P_key[$i]." = '".$_POST[$P_key[$i]]."'";
						//if($i<(count($P_key)-3))$delquery = $delquery .",";
	
						}
//				echo $delquery;
				mysql_query($delquery) or die("<p>delete niet uitgevoerd <a href='productbeheer.php' >terug</a>  </p></br>".mysql_error());
				
				}
					
		
				elseif($_POST['operation']=='insert'){
					$insquery = $insquery ."( ". /*$P_key[0]." = */"'".$_POST[$P_key[0]]."' , ";
					
					for($i = 1;$i<(count($P_key)-2); $i++ ){
					$insquery = $insquery ."  ". /*$P_key[$i]." = */"'".$_POST[$P_key[$i]]."'";
						if($i<(count($P_key)-3))$insquery = $insquery.",";
						}
						$insquery=$insquery.")";
//				echo $insquery ;
				mysql_query($insquery ) or die("<p>insert niet uitgevoerd <a href='productbeheer.php' >terug</a>  </p></br>".mysql_error());
		
				}

				else{
				
//				echo" else ja </br>";
			
					for($i = 0;$i<(count($P_key)-2); $i++ ){
					$updquery = $updquery ." ". $P_key[$i]." = '".$_POST[$P_key[$i]]."'";
						
						if($i<(count($P_key)-3)) {$updquery = $updquery .",";}
	
						}
				$updquery =$updquery . ' WHERE '.$_POST['operation']; 
				
				
				/*
				for($i = 0;$i<(count($P_key)-2); $i++ ){
				//***dit klopt niet omdat je orginel waardes niet behoud om constraint te bewaren 
				$updquery = $updquery ." AND ". $P_key[$i]." = ".$_POST[$P_key[$i]].",";
				}*/
//			echo $updquery;
			mysql_query($updquery) or die("<p>update niet uitgevoerd <a href='productbeheer.php' >terug</a></p></br>".mysql_error());
			//echo mysql_error($con );
			
			}
			}
}



/**
begin van lijst tabel
*/
if(isset($table)){

$query = mysql_query("SELECT *  FROM ".$table." "." limit ".$offset.",".$rowcount) or die("foute select stm");


	while ($row = mysql_fetch_assoc($query)) {
	
	   // form moet een name hebben 
	echo "	<form method=POST action='productbeheer.php'>";
	echo "	<table border='1'> ";
	
	$stm=" ";
	$i = 0;
	
		foreach ($row as $kolom => $value){
		
		//is voor de SQL UPDATE set, where conditie 
		$stm=$stm." ".$kolom." ="."\"".$value."\""." ";
			if ($i < (mysql_num_fields($query)-1)){
				$stm=$stm." AND "; $i++;                  
				}
			
		echo"<tr>";  
		echo"<td>".$kolom."</td>";   
		echo"<td><input type='text' name='" . $kolom . "' value=" . $value . "></td>";
		echo"</tr>";
		}
	
	echo"<tr><td><input name='operation' type='radio' value='".$stm."' />update</br> <input name='operation' type='radio' value='del' />delete </br><input name='operation' type='radio' value='insert' />insert</td><td><input type='submit' name='submit' value='submit'></td></tr>";
		
	echo"</table>";
	echo"</form></br>";
	echo "</br>";
//	echo $stm;
	   
	
							/*  
								aanntal rijren /5= aantal pagina's
								pagani nr * 5 is = offset
								
							$j = mysql_num_rows(mysql_query("SELECT *  FROM ".$table." "));
							echo $j;
							for ($i=0;$i<$j;$i++){
								
							}*/
						
	
}

	
}

?>
</body>

</html>
